/**
 * 
 */
package org.ycsoft.metroid;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


/**
 * @author lookouster
 *
 */
public class RepoXMLHandler extends DefaultHandler{
	
	RepoRequestedItem aRequest;
	RepoMatchedItem aResult;
	//RequestB is true if we're in <query></query> same for result.
	boolean requestB=false;   //Indicates that we're in the request section
	boolean resultB=false; //Indicates that we're in the queryResults section
	boolean cityB=false;  //Indicates that we're reading a city
	boolean meanB=false;  //Indicates that we're reading a mean
	boolean dateB=false;  //Indicates that we're reading a date
	boolean urlB=false;  //Indicates that we're reading an url
	
	private List<RepoMatchedItem> parsedData;
	

	/**
	 * @return the parsedData
	 */
	public List<RepoMatchedItem> getParsedData() {
		return parsedData;
	}
	
	public RepoRequestedItem getRequest()
	{
		return aRequest;
	}
	/**
	 * Evenement envoye au demarrage du parse du flux xml.
	 * @throws SAXException en cas de probleme quelquonque ne
	 *  permettant pas de
	 * se lancer dans l'analyse du document.
	 * @see org.xml.sax.ContentHandler#startDocument()
	 */
	@Override
	public void startDocument() throws SAXException {
		aRequest=new RepoRequestedItem();
	}
	
	/**
	 * Evenement envoye a la fin de l'analyse du flux xml.
	 * @throws SAXException en cas de probleme quelquonque ne permettant pas de
	 * considerer l'analyse du document comme etant complete.
	 * @see org.xml.sax.ContentHandler#endDocument()
	 */
	@Override
	public void endDocument() throws SAXException {
	        //Rien � faire
	}
	
	
	
	/**
	 * Evenement recu a chaque fois que l'analyseur rencontre une balise xml ouvrante.
	 * @param nameSpaceURI l'url de l'espace de nommage.
	 * @param localName le nom local de la balise.
	 * @param rawName nom de la balise en version 1.0 <code>nameSpaceURI + ":" + localName</code>
	 * @throws SAXException si la balise ne correspond pas a ce qui est attendu,
	 * comme par exemple non respect d'une dtd.
	 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	@Override
	public void startElement
	(String nameSpaceURI, String localName, String rawName, Attributes attributs) 
	throws SAXException {
	    if(localName.equalsIgnoreCase("query"))
	    {
	    	requestB=true;
	    }
	    else if(localName.equalsIgnoreCase("results"))
	    {
	    	//nothing to do
	    }
	    
	    else if(localName.equals("match"))
	    {
	    	resultB=true;
	    	aResult=new RepoMatchedItem();
	    }
	    else if(localName.equalsIgnoreCase("city"))
	    {
	    	cityB=true;
	    }
	    else if(localName.equalsIgnoreCase("mean"))
	    {
	    	meanB=true;
	    }
	    else if(localName.equalsIgnoreCase("date"))
	    {
	    	dateB=true;
	    }
	    else if(localName.equalsIgnoreCase("url"))
	    {
	    	urlB=true;
	    }
	    else if(localName.equalsIgnoreCase("queryResults"))
	    {
	    	resultB=true;
	    	parsedData=new ArrayList<RepoMatchedItem>();
	    }
	 
	}

	/**
	 * Evenement recu a chaque fermeture de balise.
	 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void endElement(String nameSpaceURI, String localName, String rawName) throws SAXException {
		 if(localName.equalsIgnoreCase("query"))
		    {
		    	requestB=false;
		    }
		    else if(localName.equalsIgnoreCase("result"))
		    {
		    	resultB=false;
		    }
		    else if(localName.equalsIgnoreCase("city"))
		    {
		    	cityB=false;
		    }
		    else if(localName.equalsIgnoreCase("mean"))
		    {
		    	meanB=false;
		    }
		    else if(localName.equalsIgnoreCase("date"))
		    {
		    	dateB=false;
		    }
		    else if(localName.equalsIgnoreCase("url"))
		    {
		    	urlB=false;
		    }
		 
		    else if(localName.equalsIgnoreCase("match"))
		    {
		    	parsedData.add(aResult);
		    	//resultB=false;
		    }
	}
	
	/**
	 * Evenement recu a chaque fois que l'analyseur rencontre des caracteres (entre
	 * deux balises).
	 * @param ch les caracteres proprement dits.
	 * @param start le rang du premier caractere a traiter effectivement.
	 * @param length le rang du dernier caractere a traiter effectivement
	 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
	 */
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		
		if(requestB==true)
		{
			if(cityB==true)
				aRequest.setCity(new String(ch,start,length));
			if(meanB==true)
				aRequest.setMean(new String(ch,start,length));
		}
		else if(resultB)
		{
			if(cityB==true)
				aResult.setCity(new String(ch,start,length));
			if(meanB==true)
				aResult.setMean(new String(ch,start,length));
			if(dateB==true)
				aResult.setDate(new String(ch,start,length));
			if(urlB==true)
				try {
					aResult.setUrl(new String(ch,start,length));
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}
	
	}
